package umls.interfaces.pubmed.server.sax;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import umls.interfaces.pubmed.server.PubMedAbstractMapped;

public class PubmedHandler extends DefaultHandler {
	private Map<String, String> result;
	List<PubMedAbstractMapped> result_list;
	private String abstr = null;
	private String id = null;
	boolean id_flag = false, abstr_flag = false;

	public PubmedHandler() {

		result = new LinkedHashMap<String, String>();
		result_list = new LinkedList<PubMedAbstractMapped>();
	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (qName.equalsIgnoreCase("AbstractText")) {
			abstr_flag = true;
		}
		if (qName.equalsIgnoreCase("ArticleId")) {
			if (atts.getValue("IdType").equals("pubmed")) {
				id_flag = true;
			}

		}

	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (abstr_flag) {
			abstr = new String(ch, start, length);
			abstr_flag = false;
		}
		if (id_flag) {
			id = new String(ch, start, length);
			result.put(id, abstr);
			// result_list.add(arg0)
			// System.out.println("aggiunto "+id +" "+abstr);
			id_flag = false;
		}

	}

	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase("CandidatePreferred")) {

		}
	}

	public Map<String, String> getAbstract() {
		return result;
	}
}
